package ictbk.ttmk51.grapro.app;

import ictbk.ttmk51.grapro.app.common.MyActivity;
import ictbk.ttmk51.grapro.app.safefile.SafeFile;
import ictbk.ttmk51.grapro.app.safephonebook.SafePhoneBookActivity;
import ictbk.ttmk51.grapro.app.safesms.SafeSMSActivity;
import ictbk.ttmk51.grapro.app.service.GetWebservice;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends MyActivity {
	
	private Button btnPhoneBook;
	private Button btnSMS;
	private Button btnBack;
	private Button btnExit;
	private Button btnSetting;
	private Button btnFileExplorer;
	
	Handler								myHandler				= new Handler();
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_LEFT_ICON);
        setContentView(R.layout.main_screen);
        setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.backup_data);
        
        btnPhoneBook = (Button)findViewById(R.id.btnPhoneBook);
        btnSMS = (Button)findViewById(R.id.btnSMS);
        btnSetting = (Button)findViewById(R.id.btnSetting);
        btnBack = (Button)findViewById(R.id.btnBack);
        btnExit = (Button) findViewById(R.id.btnExit);
        btnFileExplorer = (Button) findViewById(R.id.btnFileExplorer);
        
        btnPhoneBook.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainActivity.this, SafePhoneBookActivity.class);
				startActivity(intent);
			}
		});
        
        btnSMS.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainActivity.this, SafeSMSActivity.class);
				startActivity(intent);
			}
		});
        
        btnFileExplorer.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainActivity.this, SafeFile.class);
				startActivity(intent);
			}
		});
        
        btnSetting.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				Intent intent = new Intent(MainActivity.this, ChangePasswordActivity.class);
//				startActivity(intent);
				customChangePasswordAlertDialog();
			}
		});
        
        btnBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
        
        btnExit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent exit_intent = new Intent(MainActivity.this, LoginActivity.class);
				exit_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				exit_intent.putExtra("EXIT", true);
				startActivity(exit_intent);
			}
		});
    }
    
    //Change Pasword
    public void customChangePasswordAlertDialog(){
    	AlertDialog.Builder changePasswordScreen = new AlertDialog.Builder(this);
    	changePasswordScreen.setTitle("Change Password");
    	changePasswordScreen.setIcon(R.drawable.change_password);
    	
    	LayoutInflater vi = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
    	View content = vi.inflate(R.layout.change_password_screen, null);
    	
    	final EditText inputBoxOldPassword = (EditText)content.findViewById(R.id.txtOldPassword);
		final EditText inputBoxNewPassword = (EditText)content.findViewById(R.id.txtNewPassword);
		final EditText inputBoxConfirm = (EditText)content.findViewById(R.id.txtConfirmPassword);
    	
    	changePasswordScreen.setView(content);
    	changePasswordScreen.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				String old_password = inputBoxOldPassword.getText().toString();
				final String new_password = inputBoxNewPassword.getText().toString();
				String confirm_password = inputBoxConfirm.getText().toString();
				if (new_password.equals("") || confirm_password.equals("")) {
					showMessageDialog(null, "password isn't empty");
				} else {
					if (!LoginActivity.getPassword().equals(old_password))
						showToast("Old password incorrect");
					else {
						if (!new_password.equals(confirm_password))
							showToast("Password confirm incorrect");
						else {
							final ProgressDialog pd = showProgressDialog("Please waiting...", false);
							new Thread(new Runnable() {

								@Override
								public void run() {
									try {
										if (GetWebservice.changePassword(LoginActivity.getUsername(), new_password)) {
											myHandler.post(new Runnable() {
												public void run() {
													showToast("Your password changed");
												}
											});
										} else {
											myHandler.post(new Runnable() {
												public void run() {
													showToast("Have an error when attempting to change password!");
												}
											});
										}
										myHandler.post(new Runnable() {
											public void run() {
												pd.dismiss();
											}
										});
									} catch (final Exception e) {
										myHandler.post(new Runnable() {
											public void run() {
												pd.dismiss();
												showErrorDialog("System: " + e.getMessage());
											}
										});
									}
								}
							}).start();
						}

					}
				}
			}
		});
    	
    	changePasswordScreen.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
    	
    	changePasswordScreen.show();
    }
}